var serverData = {};
serverData.key = '';
var serverTable;
jQuery(document).ready(function() {
        serverTable = jQuery('#dataTable').DataTable(
									{
										"serverSide" : true,
										"ordering" : false,
										"ajax" : {
											"dataType" : "json",
											"type" : "get",
											"cache" : false,
											"url" : window.rootPath
													+ '/ui/chanceQuery/getpage/hql',// '/ui/chanceQuery/getpage',
											"data" : function(d) {
												return $.extend({}, d,
														serverData);
											}
										},
										columns : [
												{
													data : null,
													defaultContent : ""
												},
												{
													data : null,
													render : function(data,type, full, meta) {
														var operate = '<input type="checkbox" class="checkchild">';
														var id = data.id;
														return operate;

													}
												},
												{
									            	 data: null,
													 render: function(data, type, full, meta){
														var dOut;
									                    var isSucceed = data.isSucceed;
									                    if(isSucceed != null){
									                		dOut ='<p><span class="badge" style="background-color:red;font-family:arial;color:bule">已关闭-'+data.isSucceed+'</span></p>';
									                	}
									                    else{
									                    	dOut = '<p><span class="badge">处理中</span></p>';
									                    }
									                    return dOut;
													 }
									            },
												{
													data : 'name',
									                render:function(data,type,full,meta){
									                	if(data == null || data == undefined || data == ""){
															return data;
														}else{
															return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
														}
													}
												},
												{
													data : 'chancePriority'
												},
												{
													data : 'source'
												},
												{
													data : 'sourceUnit',
									                render:function(data,type,full,meta){
									                	if(data == null || data == undefined || data == ""){
															return data;
														}else{
															return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
														}
													}
												},
												{
													data : 'business',
									                render:function(data,type,full,meta){
									                	if(data == null || data == undefined || data == ""){
															return data;
														}else{
															return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
														}
													}
												},
												{
													data : 'sourceOfferInfo',
									                render:function(data,type,full,meta){
									                	if(data == null || data == undefined || data == ""){
															return data;
														}else{
															return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
														}
													}
												},
												{
													data : 'projectState',
									                render:function(data,type,full,meta){
									                	if(data == null || data == undefined || data == ""){
															return data;
														}else{
															return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
														}
													}
												},
												{
													data : 'deviceState',
									                render:function(data,type,full,meta){
									                	if(data == null || data == undefined || data == ""){
															return data;
														}else{
															return (data.length > 13) ? '<font title="'+data+'">'+data.substring(0,13)+'...'+'</font>' : data;
														}
													}
												},
												{
													data : 'sourcePower'
												},
												{
													data : 'orderAmount'
												},
												{
													data : 'orderUnit',
									                render:function(data,type,full,meta){
									                	if(data == null || data == undefined || data == ""){
															return data;
														}else{
															return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
														}
													}
												},
												{
													data : 'orderPerson'
												},
												{
													data : 'orderUnitAddr',
									                render:function(data,type,full,meta){
									                	if(data == null || data == undefined || data == ""){
															return data;
														}else{
															return (data.length > 10) ? '<font title="'+data+'">'+data.substring(0,10)+'...'+'</font>' : data;
														}
													}
												},
												{
													data : 'user.empLogin.userName'
												},
												{
													data : 'createTime',
													render : function(data,
															type, full, meta) {
														if (data) {
															return moment(data)
																	.format(
																			"YYYY-MM-DD");
														}
													}
												}, {
													data : 'memo',
									                render:function(data,type,full,meta){
									                	if(data == null || data == undefined || data == ""){
															return data;
														}else{
															return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
														}
													}
												} ],
										"drawCallback" : function(oSettings) {
											var k = 0;
											var total = oSettings.aiDisplay.length
													+ oSettings._iDisplayStart;

											for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
												$(
														'td:eq(0)',
														oSettings.aoData[oSettings.aiDisplay[k]].nTr)
														.text(i + 1);
												k++;
											}

											serverTable.off('click', 'tr');
											serverTable
													.on(
															'click',
															'tr',
															function(event) {
																event = event
																		|| window.event;
																var target = event.target
																		|| event.scrElement;
																var data = serverTable
																		.row(
																				this)
																		.data();

																return;
															});
										}
									});

					// 查看详情
					jQuery('.detailBtn').on(
							'click',
							function() {
								var selects = getSelect("#dataTable", 1);
								if (selects.length > 1) {
									$.springLemonDialog({
										content : '只能操作一条记录',// 确认文本
										clickButton : function(sender, modal,
												index) {// 按钮事件处理
											$(this).closeDialog(modal);
										}
									});
								} else if (selects.length == 0) {
									$.springLemonDialog({
										content : '请选择要操作的记录',// 确认文本
										clickButton : function(sender, modal,
												index) {// 按钮事件处理
											$(this).closeDialog(modal);
										}
									});
								} else if (selects.length == 1) {
									var id = selects[0].id;
									window.location = window.rootPath
											+ "/ui/chance/detail"
											+ "?chanceId=" + id;
								}
							});

					// 查询
					jQuery('.searchBtn').on('click', function() {	
						
						if(jQuery('#isSucceed').val().trim()) serverData.isSucceed = jQuery('#isSucceed').val();  else serverData.isSucceed=null;
						if(jQuery('#name').val().trim()) serverData.name = jQuery('#name').val();  else serverData.name=null;
						if(jQuery('#chancePriority').val().trim()) 	serverData.chancePriority= jQuery('#chancePriority').val();  else serverData.chancePriority=null;
						if(jQuery('#source').val().trim()) 	serverData.source= jQuery('#source').val();  else serverData.source;
						if(jQuery('#sourceUnit').val().trim()) 	serverData.sourceUnit= jQuery('#sourceUnit').val() ; else serverData.sourceUnit=null;
						if(jQuery('#projectState').val().trim()) 	serverData.projectState= jQuery('#projectState').val();  else serverData.projectState=null;
						if(jQuery('#deviceState').val().trim()) 	serverData.deviceState= jQuery('#deviceState').val();  else serverData.deviceState=null;
						if(jQuery('#sourcePower').val().trim()) 	serverData.sourcePower= jQuery('#sourcePower').val() ; else serverData.sourcePower=null;
						if(jQuery('#orderAmount').val().trim()) 	serverData.orderAmount= jQuery('#orderAmount').val();  else serverData.orderAmount=null;
						if(jQuery('#orderUnit').val().trim()) 	serverData.orderUnit= jQuery('#orderUnit').val();  else serverData.orderUnit=null;
						if(jQuery('#orderPerson').val().trim()) 	serverData.orderPerson= jQuery('#orderPerson').val();  else serverData.orderPerson=null;
						if(jQuery('#orderUnitAddr').val().trim()) 	serverData.orderUnitAddr= jQuery('#orderUnitAddr').val();  else serverData.orderUnitAddr=null;
						if(jQuery('#creator').val().trim()) 	serverData.creator= jQuery('#creator').val();  else serverData.creator=null;
                        if(jQuery('#createTimeStartDate').val()) serverData.createTimeStartDate = jQuery('#createTimeStartDate').val();  else serverData.createTimeStartDate=null;
                        if(jQuery('#createTimeEndDate').val()) serverData.createTimeEndDate = jQuery('#createTimeEndDate').val();  else serverData.createTimeEndDate=null;
						serverTable.draw();
						
                    });
					//查询关键字获取、
					
					var getSearchKeyWord=function(){
						var keyword=null;
						if(jQuery('#isSucceed').val().trim()) keyword=keyword+"+isSucceed+"+jQuery('#isSucceed').val();
						if(jQuery('#name').val().trim()) keyword=keyword+"+name+"+jQuery('#name').val();
						if(jQuery('#chancePriority').val().trim()) keyword=+keyword+"+chancePriority+"+jQuery('#chancePriority').val();
						if(jQuery('#source').val().trim()) 	keyword=keyword+"+source+"+jQuery('#source').val();
						if(jQuery('#sourceUnit').val().trim()) 	keyword=keyword+"+sourceUnit+"+jQuery('#sourceUnit').val();
						if(jQuery('#projectState').val().trim()) 	keyword=keyword+"+projectState+"+jQuery('#projectState').val();
						if(jQuery('#deviceState').val().trim()) 	keyword=keyword+"+deviceState+"+jQuery('#deviceState').val();
						if(jQuery('#sourcePower').val().trim()) 	keyword=keyword+"+sourcePower+"+jQuery('#sourcePower').val();
						if(jQuery('#orderAmount').val().trim()) 	keyword=keyword+"+orderAmount+"+jQuery('#orderAmount').val();
						if(jQuery('#orderUnit').val().trim()) 	keyword=keyword+"+orderUnit+"+jQuery('#orderUnit').val();
						if(jQuery('#orderPerson').val().trim()) 	keyword=keyword+"+orderPerson+"+jQuery('#orderPerson').val();
						if(jQuery('#orderUnitAddr').val().trim()) 	keyword=keyword+"+orderUnitAddr+"+jQuery('#orderUnitAddr').val();
						if(jQuery('#creator').val().trim()) 	keyword=keyword+"+creator+"+jQuery('#creator').val();
                        if(jQuery('#createTimeStartDate').val())	keyword=keyword+"+createTimeStartDate+"+jQuery('#createTimeStartDate').val();
                        if(jQuery('#createTimeEndDate').val()) 	keyword=keyword+"+createTimeEndDate+"+jQuery('#createTimeEndDate').val();
					    return keyword;
					}
					// 撤销查询
					jQuery('.resetBtn').on("click", function() {
						clearQueryServerData('clearui');
						serverTable.draw();
					});
					// /////列表中的全选
					$("#checkAll").click(function() {
						var check = $(this).prop("checked");
						$(".checkchild").prop("checked", check);
					});
					// 导出
					jQuery(".export").on(
									"click",
									function() {
										var selects = getSelect("#dataTable", 1);
										if (selects.length > 0) {
											var key = selects[0].id;
											for (var i = 1; i < selects.length; i++) {
												key += "," + selects[i].id;
											}
											var url = window.rootPath
													+ "/ui/chanceQuery/exportbyselect/"
													+ key;
											window.location = url;
										} else {	
											var key=getSearchKeyWord();
											if (!key)
												key = 'null';
											var url = window.rootPath
													+ "/ui/chanceQuery/exportbykeyword/"
													+ key;
											window.location = url;
										
//											jQuery.ajax({
//												type : 'post',
//												url : window.rootPath+ "/ui/chanceQuery/exportbykeyword",
//												dataType : "json",
//												cache : false,
//												data :function(d) {
//													return $.extend({}, d,serverData);
//												},
//											success : function(data) {
//											serverTable.draw();
//												}	,
//											    error : function() {  
//											    		jQuery("#loadingImage").hide();
//											            } 
//											});
										}
									});
									jQuery(".printnumberBtn").on( "click", function() {
										jQuery('#InvoiceEffectiveDateModal').modal('show');
									});
									jQuery(".savedInvoiceEffectiveDateBtn").on( "click", function() {
										var year = jQuery("#sel").val();
										var url = window.rootPath + "/ui/chanceQuery/printnumber/"+year;
										window.location = url;
									});
									var sel = document.getElementById('sel');
					var date = new Date();
					for(var j=2;j>=0;j--){
						var opt = document.createElement("option");
						opt.value = date.getFullYear()+j;
						opt.innerHTML = (date.getFullYear()+j);
						sel.appendChild(opt);
						if(j==0){
							opt.selected=true
						}
					}
					for(var i=0;i<5;i++)
					{
						var opt = document.createElement("option");
						opt.value = date.getFullYear()-i-1;
						opt.innerHTML = (date.getFullYear()-i-1);
						sel.appendChild(opt);
					}
				             });

function clearQueryServerData(option){
	if(serverData.isSucceed) serverData.isSucceed = null;
    if(serverData.name) serverData.name = null;
    if(serverData.chancePriority) 	serverData.chancePriority=  null;
    if(serverData.source) 	serverData.source=  null;
    if(serverData.sourceUnit) 	serverData.sourceUnit=  null;
    if(serverData.projectState) 	serverData.projectState=  null;
    if(serverData.deviceState) 	serverData.deviceState= null;
    if(serverData.sourcePower) 	serverData.sourcePower=  null;
    if(serverData.orderUnit) 	serverData.orderUnit=  null;
    if(serverData.orderPerson) 	serverData.orderPerson= null;
    if(serverData.orderUnitAddr) 	serverData.orderUnitAddr= null;
    if(serverData.creator) 	serverData.creator= null;
    if(serverData.createTimeStartDate) serverData.createTimeStartDate =  null;
    if(serverData.createTimeEndDate) serverData.createTimeEndDate = null;
	if(option=='clearui') jQuery('input').val('');jQuery('select').val('')
    
}